<!--:include("start.html"){}  -->
<link href="https://cdn.bootcss.com/bootstrap-select/2.0.0-beta1/css/bootstrap-select.min.css" rel="stylesheet">
                <div class="row">
                    <div class="col-12">
                        <div class="card">
                            <div class="card-body">
                                <h4 class="card-title">${isEmpty(obj.id)?"新增":"编辑"}试题</h4>
                                <h6 class="card-subtitle"></h6>
                                <div class="m-t-40">
                                        <div class="d-flex">
                                            <div class="ml-auto">
                                                <div class="form-group">
                                                	<!--:if(isEmpty(obj.id)){  -->
                                                    <button id="addform" data-toggle="modal" data-target="#add-contact" class="btn btn-primary btn-sm"><i class="icon wb-plus" aria-hidden="true"></i>
                                                    	复制题目
                                                    </button>
                                                    <!--:}  -->
                                                </div>
                                            </div>
                                        </div>
                                </div>
                                <div class="copyform">
<form action="save" id="form0" method="post" >
<div class="form-group row">
  <label for="example-text-input" class="col-sm-2 text-right control-label col-form-label">标题</label>
  <div class="col-10">
  	<input type="hidden" name="id" value="${obj.id!}" />
  	<input type="hidden" name=".table" value="cp_tm" />
  	<input type="hidden" name="opby" value="${session.admin.id!0}" >
    <input class="form-control" type="text" name="title" value="${obj.title!}" >
  </div>
</div>
<div class="form-group row">
  <label for="example-text-input" class="col-sm-2 text-right control-label col-form-label">维度</label>
  <div class="col-10">
    <select name='wd_id' class='custom-select col-2'>
    	<!--:for(wd in wds!){  -->
    	<option value="${wd.id!}" ${(obj.wd_id!null)==wd.id?"selected":""}>${wd.name!}</option>
    	<!--:}  -->
    </select>
  </div>
</div>
<div class="form-group row">
  <label for="example-text-input" class="col-sm-2 text-right control-label col-form-label">选项得分</label>
  <div class="col-10">
	    <ul class="list-unstyled options">
	    <button type="button" class="btn btn-success vote-add-btn"><i class="fa fa-plus"></i>
	    	添加选项得分维度
	    </button>
		</ul>
  </div>
</div>
</form>
</div>
<div class="form-group m-b-0">
  <div class="offset-sm-3 col-sm-9">
    <button type="button" id="sub1" class="btn btn-info waves-effect waves-light m-t-10">保存</button>
    <button type="button" onclick="history.go(-1);" class="btn btn-danger waves-effect waves-light m-t-10">取消</button>
  </div>
</div>
</div>
</div>
</div>
</div>
<!--:include("end.html"){}  -->
<script src="https://cdn.bootcss.com/bootstrap-select/2.0.0-beta1/js/bootstrap-select.min.js"></script>
<script>
$(document).ready(function() {
	
	var formnum=0;
var options0=${obj.options!'[]'}
var scores0=${obj.scores!'[]'}
for (var i = 0; i < options0.length; i++) {
	var options1=options0[i]
	var scores1=scores0[i];
    $(".options").append("<li class='media'><label for='example-text-input' class='col-1 col-form-label'>选项</label><input type='text' class='form-control col-2' name='options' value='" 
    		+ options1 + "'>"+
    		"<label for='example-text-input' class='col-1 col-form-label'>得分</label><input type='number' class='form-control col-2' name='scores' value='" 
        	+ scores1 + "'>" 
    		+"<button class='btn btn-danger del-item waves-effect waves-light' type='button'><span class='btn-label'><i class='fa fa-trash-o'></i></span>删除</button></li>");
    //$('#wd_ids'+i).selectpicker('val',wds[i]);
    $(".media > .del-item").click(function() {
        $(this).parent().remove();
        for (i = $(this).index() - 1; i < num; i++) {
            newnum = i + 1;
            $(".options > li").eq(i).find("input").attr("placeholder", "填写题目选项" + newnum);
        }
    });
    
    
    
}

$(".vote-add-btn").click(function() {
    var num = $(".options > li").length;
    var xuanxiang="选项"+(num+1);
    switch (num) {
	case 0:xuanxiang="A"
		break;
	case 1:xuanxiang="B"
		break;
	case 2:xuanxiang="C"
		break;
	case 3:xuanxiang="D"
		break;
	default:
		break;
	}
    $(".options").append("<li class='media'><label for='example-text-input' class='col-1 col-form-label'>选项</label><input type='text' class='form-control col-2' name='options' value='" 
    		+ xuanxiang + "'>"+
    		"<label for='example-text-input' class='col-1 col-form-label'>得分</label><input type='number' class='form-control col-2' name='scores' value='" 
        	+ (num+1) + "'>"
    		+"<button class='btn btn-danger del-item waves-effect waves-light' type='button'><span class='btn-label'><i class='fa fa-trash-o'></i></span>删除</button></li>");
    $(".media > .del-item").click(function() {
        $(this).parent().remove();
        for (i = $(this).index() - 1; i < num; i++) {
            newnum = i + 1;
            $(".options > li").eq(i).find("input").eq(0).attr("placeholder", "填写选项" + newnum);
            $(".options > li").eq(i).find("input").eq(1).attr("placeholder", "填写得分" + newnum);
        }
    });
});
if(options0.length=="0"){
	$(".vote-add-btn").click();
	$(".vote-add-btn").click();
	$(".vote-add-btn").click();
	$(".vote-add-btn").click();
}
    $("#sub1").click(function(){
    	for(var i=0;i<formnum;i++){
    		
    		var form = new FormData(document.getElementById("form"+i));
    		　$.ajax({
    		                url:"save",
    		                type:"post",
    		                data:form,
    		                processData:false,
    		                contentType:false,
    		                success:function(data){
    		                    console.log("over.."+data);
    		                }
    		});
    	}
    	alert("提交了"+formnum+"道试题!")
    	location.href="index"
        return false;
	});
    
    $("#addform").click(function(){
    	formnum++;
    	var $form0=$("#form0").clone();
    	$form0.attr("id","form"+formnum)
    	$(".copyform").append($form0);
    })
})
</script>